import { createRouter, createWebHistory } from 'vue-router';
import Staff from "@/views/Staff.vue";


const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes: [
      {
        path: '/',
        name: 'home',
        redirect: '/gym/index',
        component: () => import('../views/Home.vue'),
        meta:{title: '跳转便捷页'}
      },
      {
      path: '/gym',
      name: 'gym',
      redirect:'gym/index',
      component: () => import('../views/Gym.vue'),
        children:[
          {
            path: 'index',
            name: 'u_index',
            component: () => import('../views/user/UserIndex.vue'),
          },
          {
            path: 'course',
            name: 'u_course',
            component: () => import('../views/user/UserCourse.vue'),
          },
          {
            path: 'record',
            name: 'u_record',
            component: () => import('../views/user/UserRecord.vue'),
            meta: { requiresAuth: true }
          },
          {
            path: 'health',
            name: 'u_health',
            component: () => import('../views/user/UserHealth.vue'),
            meta: { requiresAuth: true }
          },
          {
            path: 'about',
            name: 'u_about',
            component: () => import('../views/user/UserAbout.vue'),
          },
          {
            path: 'details',
            name: 'u_details',
            component: () => import('../views/user/UserDetails.vue'),
            meta: { requiresAuth: true }
          },
          {
            path: 'payOrders',
            name: 'u_payOrders',
            component: () => import('../views/user/UserPayOrders.vue'),
            meta: { requiresAuth: true }
          }
        ]
    },
      {
      path: '/login',
      name: 'login',
      component: () => import('../views/Login.vue'),
    },
      {
      path: '/register',
      name: 'register',
      component: () => import('../views/Register.vue'),
    },

    {
      path: '/admin',
      component: () => import('../views/Admin.vue'),
      meta: { requiresAuth: true ,role:'admin'},
      redirect: '/admin/a_index',
      children: [
        {
          path: 'a_index',
          name: 'a_index',
          meta: { title: '首页',
            requiresAuth: true },
          component: () => import('../views/AIndex.vue'),
        },
        {
          path: 'a_user',
          name: 'a_user',
          meta: { title: '会员列表页',
            parentTile: '管理员页',
            requiresAuth: true },
          component: () => import('../views/AUser.vue'),
        },
        {
          path: 'a_user/:uid', // 带参数的路由
          name: 'a_user_with_uid',
          meta: {
            title: '用户详情页',
            requiresAuth: true
          },
          component: () => import('../views/AUser.vue')
        },
        {
          path: 'a_employee',
          name: 'a_employee',
          meta: { title: '员工列表页',
            parentTile: '管理员页',
            requiresAuth: true },
          component: () => import('../views/AEmployee.vue'),
        },

        {
          path: 'a_c_introduction',
          name: 'a_c_introduction',
          meta: { title: '教练信息页',
            parentTile: '管理员页',
            requiresAuth: true },
          component: () => import('../views/ACIntroduction.vue'),
        },
        //新增带参数路径
        {
          path: 'a_employee/:eid',
          name: 'a_employee_with_eid',
          meta: { title: '管理员工页', requiresAuth: true },
          component: () => import('../views/AEmployee.vue'),
        },
        {
          path:'a_courses',
          name:'a_courses',
          meta:{title:'课程列表页',
            parentTile:'管理员页',
            requiresAuth:true},
          component:()=>import('../views/ACourses.vue'),
        },
        {
          path:'a_pay',
          name:'a_pay',
          meta:{title:'订单管理页',
            parentTile:'管理员页',
            requiresAuth:true},
          component:()=>import('../views/APayOrder.vue'),
        },
        {
          path:'a_cs_introduction',
          name:'a_cs_introduction',
          meta:{title:'课程信息页',
            parentTile:'管理员页',
            requiresAuth:true},
          component:()=>import('../views/ACSIntroduction.vue'),
        },
        //新增带参数路径
        {
          path: 'a_courses/:cid',
          name: 'a_courses_with_cid',
          meta: { title: '课程列表页',
            requiresAuth: true },
          component: () => import('../views/ACourses.vue'),
        },
        {
          path:'a_enrollment',
          name:'a_enrollment',
          meta:{title:'名单信息页',
            parentTile: '管理员页',
            requiresAuth:true},
          component:()=>import('../views/AEnrollment.vue'),
        },
        {
          path: 'a_enrollment/uid/:uid',  // 修改为明确路径
          name: 'a_enrollment_with_uid',
          meta: { title: '名单详情页', requiresAuth: true },
          component: () => import('../views/AEnrollment.vue'),
        },
          // 新增带参数路径
        {
          path: '/enrollment/:cid',
          name: 'a_enrollment_with_cid',
          meta: {title: '名单信息页',requiresAuth: true},
          component: () => import('@/views/AEnrollment.vue')
        },
        {
          path: 'a_equipment',
          name: 'a_equipment',
          meta: { title: '器材列表页',
            parentTile: '管理员页'
          , requiresAuth: true },
          component: () => import('../views/AEquipment.vue'),
        },
        {
          path: 'a_repair',
          name: 'a_repair',
          meta: { title: '维修记录页',
            parentTile: '管理员页',
            requiresAuth: true },
          component: () => import('../views/ARepairStatus.vue'),

        }
      ],
    },
    {
      path: '/coach',
      name: 'coach',
      component: ()=>import('../views/Coach.vue'),
      redirect:'/coach/mycourse',
      children:[
        {
          path:'mycourse',
          name:'mycourse',
          meta: { title: '我的课程'},
          component: () => import('../views/coach/CourseManage.vue')
        },
        {
          path:'myuser',
          name:'myuser',
          meta: { title: '我的学员'},
          component: () => import('../views/coach/UserManage.vue')
        },
        {
          path:'myconfig',
          name:'myconfig',
          meta: { title: '账户设置'},
          component: () => import('../views/coach/Config.vue')
        },
      ]
    },

    //staff
    {
      path: '/staff',
      name: 'staff',
      component: Staff,
      redirect:'/staff/index',
      children:[
        {
          path: 'index',
          name: 's_index',
          meta: { title: '首页' },
          component: () => import('../views/StaffIndex.vue')
        },
        {
          path:'user',
          name:'s_user',
          meta: { title: '查询用户页', requiresAuth: true },
          component: () => import('../views/StaffUser.vue')
        },
        {
          path: 'user/add',
          name: 's_add_user',
          meta: { title: '添加新会员' },
          component: () => import('../views/StaffAddUser.vue')
        },
        {
          path:'equipment',
          name:'s_equipment',
          meta: { title: '修改器械状态页', requiresAuth: true },
          component: () => import('../views/StaffEquipment.vue')
          },
        {
          path:'repair',
          name:'s_repair',
          meta: { title: '新增维修器械页', requiresAuth: true },
          component: () => import('../views/StaffRepair.vue')
        },
      ]
    },
    {
      path: '/user',
      name: 'user',
      meta: { title: '用户页面' },
      component: () => import('../views/User.vue'),
    },
  ],
});

router.beforeEach((to, from, next) => {
  // 设置页面标题
  if (to.meta.title) {
    document.title = to.meta.title;
  }

  // 权限控制逻辑
  const isAuthenticated = true; // 替换为实际的权限判断逻辑
  if (to.meta.requiresAuth && !isAuthenticated) {
    next({ name: 'login' });
  } else {
    next();
  }
});

export default router;
